<template>
    <div class="home_txt" v-loading.fullscreen.lock="fullscreenLoading">
        <banner :bannerList="homeData.banner"></banner>
        <div class="search_box">
            <div class="search_input">
                <input @keydown.enter='goSearch(searchTxt)' v-model="searchTxt" @input="SearchLsit" placeholder="搜索您要的产品型号或关键字"/>
                <div class="search_btn" @click="goSearch(searchTxt)">搜索</div>
                <div class="searc_box_chang" v-if="searchList.length">
                    <div @click="goSearch(item.name)" v-for="(item,index) in searchList" :key="index" v-html="item.name"></div>
                </div>
            </div>
        </div>
        <div class="group_info">
            <div class="group_infos" v-for="(item,index) in homeData.group" :key="index">
                <div class="group_changs">
                    <div class="group_info_content" @click="goGroupTwo(item)">
                        <div class="group_one_title">
                            <img :src="item.icon">
                            <div class="titles">{{ item.name }}</div>
                        </div>
                        <div class="group_json">{{ item.info }}</div>
                    </div>
                    <div class="more_infos" @click="openGroupList(index)" v-if="item.list.length>5"> {{item.list.length>item.openNum?'查看更多':'收起'}}</div>
                    <!--          <div class="more_infos" v-if="item.list.length>5">-->
                    <!--            <img class="select" src="../assets/more-selected.png">-->
                    <!--            <img class="uncheck" src="../assets/more-uncheck.png">-->
                    <!--          </div>-->
                </div>
                <div class="group_box">
                    <div class="info_groups" @click="goGroupTwo(items)" v-for="(items,indexs) in item.list.slice(0,item.openNum)" :key="indexs">
                        <div class="group_img">
                            <img :src="items.cover" class="img_box">
                        </div>
                        <div class="group_titles">{{ items.name }}</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import Banner from "@/components/banner";
    import {getHomeData, searchGoods} from "@/api/apis";

    export default {
        name: "home",
        components: {Banner},
        data() {
            return {
                homeData: {},
                searchList: [],
                searchTxt: '',
                fullscreenLoading: false
            }
        },
        methods: {
            openGroupList(e) {
                let obj = this.homeData.group[e]
                if (obj.list.length > obj.openNum) {
                    obj.openNum = obj.length
                } else {
                    obj.openNum = 5
                }
            },
            SearchLsit() {
                let data = {name: this.searchTxt, type: 1}
                searchGoods(data).then(res => {
                    // if(res.data.length){
                    //   res.data.map(item=>{
                    //     if(item.name.includes(this.searchTxt)){
                    //       item.name = item.name.replace(item.name,'<span style="color:#B11E24">' + item.name + '</span>')
                    //     }
                    //   })
                    // }
                    this.searchList = res.data
                })
            },
            goSearch(e) {
                this.$router.push({name: 'search', query: {name: e}})
            },
            GetHomeData() {
                this.fullscreenLoading = true
                getHomeData().then(res => {
                    if (res.data.group.length) {
                        res.data.group.map(item => {
                            item.openNum = 5
                        })
                    }
                    this.homeData = res.data
                    this.fullscreenLoading = false
                })
            },
            goGroupTwo(e) {
                console.log(e);
                if (e.is_end) {
                    this.$router.push({name: 'filterGroups', query: {group_id: e.id}})
                } else {
                    this.$router.push({name: 'groups', query: {group_id: e.id}})
                }
            }
        },
        mounted() {
            this.GetHomeData()
        }
    }
</script>

<style scoped lang="less">
    .home_txt {
        .search_box {
            margin: 60px auto 40px;
            
            .search_input {
                cursor: pointer;
                width: 536px;
                height: 42px;
                margin: 0 auto;
                background: #FFFFFF;
                box-sizing: border-box;
                display: flex;
                align-items: center;
                position: relative;
                
                input {
                    width: 463px;
                    height: 42px;
                    background: #FFFFFF;
                    border: 1px solid #000000;
                    box-sizing: border-box;
                    padding: 0 23px;
                    font-size: 14px;
                    font-family: Microsoft YaHei;
                    font-weight: 400;
                }
                
                .search_btn {
                    width: 73px;
                    height: 42px;
                    background: #000000;
                    font-size: 14px;
                    font-family: Microsoft YaHei;
                    font-weight: 400;
                    line-height: 42px;
                    color: #FFFFFF;
                    text-align: center;
                }
                
                .searc_box_chang {
                    width: 463px;
                    position: absolute;
                    top: 42px;
                    padding: 13px 23px 8px;
                    background: #FFFFFF;
                    border: 1px solid #000000;
                    box-sizing: border-box;
                    border-top-color: #FFFFFF;
                    
                    div {
                        font-size: 14px;
                        font-family: Microsoft YaHei;
                        font-weight: 400;
                        color: #9D9D9D;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        white-space: nowrap;
                        margin-bottom: 10px;
                    }
                    
                    div:hover {
                        color: #B11E24;
                    }
                }
            }
        }
        
        .group_info {
            width: 1200px;
            margin: 0 auto;
            cursor: pointer;
            
            .group_changs {
                display: flex;
                justify-content: space-between;
                align-items: center;
                
                .group_info_content {
                    width: 80%;
                    
                    .group_one_title {
                        display: flex;
                        align-items: center;
                        margin-bottom: 9px;
                        
                        img {
                            width: 30px;
                            height: 28px;
                            margin-right: 12px;
                        }
                        
                        .titles {
                            font-size: 20px;
                            font-family: Microsoft YaHei;
                            font-weight: bold;
                            color: #000000;
                        }
                    }
                    
                    .group_json {
                        font-size: 14px;
                        font-family: Microsoft YaHei;
                        font-weight: 400;
                        color: #9D9D9D;
                        margin-bottom: 28px;
                    }
                }
                
                .more_infos {
                    width: 58px;
                    height: 24px;
                    border: 1px solid #000000;
                    box-sizing: border-box;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    font-size: 12px;
                    text-align: center;
                    line-height: 24px;
                    
                    img {
                        width: 30px;
                        height: 7px;
                    }
                    
                    .select {
                        display: none;
                    }
                    
                    .uncheck {
                        display: block;
                    }
                }
                
                .more_infos:hover {
                    color: #B11E24;
                    border-color: #B11E24;
                    
                    .select {
                        display: block;
                    }
                    
                    .uncheck {
                        display: none;
                    }
                }
            }
            
            .group_box {
                display: flex;
                flex-wrap: wrap;
                
                .info_groups {
                    width: 217px;
                    margin-right: 28px;
                    margin-bottom: 28px;
                    
                    .group_img {
                        height: 150px;
                        background: rgba(0, 0, 0, 0);
                        border: 1px solid #E9E9E9;
                        box-sizing: border-box;
                        
                        .img_box {
                            width: 100%;
                            height: 100%;
                            object-fit: cover;
                        }
                    }
                    
                    .group_titles {
                        text-align: center;
                        font-size: 14px;
                        font-family: Microsoft YaHei;
                        font-weight: 400;
                        color: #000000;
                        margin-top: 10px;
                    }
                }
                
                .info_groups:nth-child(5n) {
                    margin-right: 0;
                }
                
                .info_groups:hover .group_img {
                    border-color: #B11E24;
                    transition: all 0.3s linear;
                }
                
                .info_groups:hover .group_titles {
                    color: #B11E24;
                    transition: all 0.3s linear;
                }
            }
            
            
        }
    }
</style>
